(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:IsvstoreCtrl
     * @description
     * # IsvstoreCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('IsvstoreCtrl', IsvstoreCtrl);

    IsvstoreCtrl.$inject = ['$scope', '$rootScope', '$state', 'dataService', '$location', 'helper', '$timeout', 'sessionService', 'cacheService', 'ERP_CONFIG', '$compile', 'moment', 'base64'];

    function IsvstoreCtrl($scope, $rootScope, $state, dataService, $location, helper, $timeout, sessionService, cacheService, ERP_CONFIG, $compile, moment, base64) {
        $scope.listAction = function() {
            $scope.getstoreList();
        }

        //获取store列表
        $scope.getstoreList = function(pageNo) {
            var query = {
                order_by: "store_id:desc",
                page_no: pageNo ? pageNo : 1,
                page_size: $scope.page_size ? $scope.page_size : 10
            };
            var q = [];
            $scope.storeList = [];
            dataService.storeGet(query).then(function(list) {
                if (list && list.store) {
                    async.map(list.store, function(data, callback) {
                        var query = {
                            partner_id: data.partner_id
                        };
                        dataService.v1PartnerGet(query).then(function(lit) {
                            if (!lit.Msg) {
                                data.partner = lit;
                                callback(null, null);
                            } else {
                                callback(null, null);
                            }
                        });
                    }, function(e, r) {
                        $scope.storeList = list.store;
                        $scope.storeTotal = list.total_count;
                        //                         console.error("$scope.storeList", $scope.storeList);
                    });
                }
            });
        }

        // v1PartnerGet
        $scope.infoAction = function() {
            var store_id = $location.search().store_id;
            if (store_id) {
                dataService.storeOne(store_id).then(function(retun) {
                    if (retun) {
                        $scope.store = retun;
                    }
                });
            }
        }


        $scope.groupaddAction = function() {
            $scope.partner_id = $location.search().partner_id;
            if ($scope.partner_id) {
                var query = {
                    partner_id: $scope.partner_id
                };
                dataService.getStoredistributionGroup(query).then(function(list) {
                    if (list && list.total_count) {
                        $scope.group = list.fz_list[0];
                        console.error("store", $scope.group);
                    }
                });
            }
        }

        $scope.groupAdd = function() {
            if ($scope.partner_id) {
                $scope.group.partner_id = $scope.partner_id;
                $scope.group.is_admin = 2;
                // console.error("store", $scope.group);
                // return false;
                dataService.getStoredistributionAdd($scope.group).then(function(a) {
                    if (a.Msg) {
                        helper.myAlert(a.Msg);
                    } else {
                        helper.myAlert("success");
                        $scope.getstoreList();
                    }
                });
            }
        }
    }
})();